package com.ng.framework.view.desktop.table;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.apache.commons.collections.CollectionUtils;

public class NGCustomTableHeader extends JTableHeader {

	private static final long serialVersionUID = 4057362460546883447L;

	protected List<NGColumnGroup> columnGroups;

	public NGCustomTableHeader(TableColumnModel model) {
		super(model);

		this.setUI(new NGCustomTableHeaderUI());
		this.setReorderingAllowed(false);
	}

	public void setReorderingAllowed(boolean b) {
		reorderingAllowed = false;
	}

	public void addColumnGroup(NGColumnGroup columnGroup) {
		if (columnGroups == null) {
			this.columnGroups = new ArrayList<NGColumnGroup>();
		}
		columnGroups.add(columnGroup);
	}

	public List<Object> getColumnGroups(TableColumn tableColumn) {
		List<Object> columnGroups = new ArrayList<Object>();
		
		if(this.columnGroups != null) {
			for (NGColumnGroup columnGroup : this.columnGroups) {
				columnGroups = columnGroup.getColumnGroups(tableColumn,
						new ArrayList<Object>());
				if(CollectionUtils.isNotEmpty(columnGroups)) {
					break;
				}
			}			
		}

		return columnGroups;
	}

	public void setColumnMargin() {
		if (columnGroups != null) {
			int columnMargin = getColumnModel().getColumnMargin();

			for (NGColumnGroup columnGroup : columnGroups) {
				columnGroup.setColumnMargin(columnMargin);
			}
		}
	}
}
